/**
 * 
 */
package ext.ux.files.client.dialog.ui;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.ToolBarEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.toolbar.AdapterToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.TextToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;

import ext.ux.files.client.dialog.controler.AddressControler;
import ext.ux.files.client.dialog.controler.FilesEvents;
import ext.ux.files.client.dialog.model.FileModel;

/**
 * The panel which contains the adress bar, history's buttons
 * 
 * @author Eric Taix
 */
public class AddressPanel extends ToolBar {

	// The history list
	private List<FileModel> history = new ArrayList<FileModel>();
	// The current index
	private int currentHistory;

	// Adress field
	private TextField<String> address;
	// Previous button
	private TextToolItem prev;
	// Next button
	private TextToolItem next;
	// Flag to prevent from adding to history
	private boolean fromNavigation = false;

	/**
	 * Constructor
	 */
	public AddressPanel() {
		initUI();
		// 
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.addController(new AddressControler(this));
	}

	/**
	 * Initialize the user interface
	 */
	protected void initUI() {
		addStyleName("address-tb");
		prev = new TextToolItem();
		prev.setIconStyle("icon-previous");
		prev.setEnabled(false);
		prev.addListener(Events.Select, new Listener<ToolBarEvent>() {
			public void handleEvent(ToolBarEvent be) {
				FileModel model = history.get(currentHistory - 1);
				currentHistory--;
				updateHistory();
				fromNavigation = true;
				FilesEvents.fireDirectoryChanged(model);
			}
		});
		add(prev);
		next = new TextToolItem();
		next.setIconStyle("icon-next");
		next.setEnabled(false);
		next.addListener(Events.Select, new Listener<ToolBarEvent>() {
			public void handleEvent(ToolBarEvent be) {
				FileModel model = history.get(currentHistory + 1);
				currentHistory++;
				updateHistory();
				fromNavigation = true;
				FilesEvents.fireDirectoryChanged(model);
			}
		});
		add(next);
		// Adress
		add(new SeparatorToolItem());
		address = new TextField<String>();
		address.setReadOnly(true);
		AdapterToolItem adapt = new AdapterToolItem(address);
		address.setWidth("100%");
		address.setValue("\\");
		adapt.setData("width", "100%");
		add(adapt);
		// initialize the history
		initHistory();
	}

	/**
	 * Initialize (clear) the history list
	 */
	public void initHistory() {
		history = new ArrayList<FileModel>();
		next.setEnabled(false);
		prev.setEnabled(false);
	}

	/**
	 * Add a new history
	 * 
	 * @param historyP
	 */
	private void addHistory(FileModel modelP) {
		if (!fromNavigation) {
			history.add(modelP);
			currentHistory = history.size() - 1;
			updateHistory();
		}
		fromNavigation = false;
	}

	/**
	 * Update navigation buttons state and tips
	 */
	private void updateHistory() {
		if (currentHistory < (history.size() - 1)) {
			next.setEnabled(true);
			next.setTitle(history.get(currentHistory + 1).getAbsoluteName());
		}
		else {
			next.setEnabled(false);
			next.setTitle("");
		}
		if (currentHistory > 0) {
			prev.setEnabled(true);
			prev.setTitle(history.get(currentHistory - 1).getAbsoluteName());
		}
		else {
			prev.setEnabled(false);
			prev.setTitle("");
		}
	}

	/**
	 * Change the current history
	 */
	public void changeDirectory(FileModel newDirP) {
		if (newDirP == null) {
			address.setValue("\\");
		}
		else {
			String dir = newDirP.getAbsoluteName();
			dir = dir.replace("\\", " \\ ").replace("/", " / ");
			address.setValue(dir);
		}
		addHistory(newDirP);
	}

}
